package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserActivityRealReward;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserActivityRealRewardDaoImpl extends TempDataObjectDao<UserActivityRealReward> {

    public Map<Integer, List<UserActivityRealReward>> getUserActivityRealReward(List<Integer> activityIdList, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String activityTypeCondition = "activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        String sql = "select * from t_u_activity_real_reward where " + activityTypeCondition + " and `userId`=?";
        Map<Integer, List<UserActivityRealReward>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityRealReward data = parse(rs);
                if(!result.containsKey(data.getActivityId())){
                    result.put(data.getActivityId(), new ArrayList<>());
                }
                result.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateUserActivityRealRewardSql = "update `t_u_activity_real_reward` set goodsId=?,getTime=?,addrTime=?,addr=?,phone=? where activityId=? and userId=? and no=?";
    @Override
    protected boolean updateData(UserActivityRealReward userActivityRealReward) {
        userActivityRealReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getGoodsId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userActivityRealReward.getGetTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userActivityRealReward.getAddrTime().getTime())));
        para.put(i++, new DbParameter(Types.VARCHAR, userActivityRealReward.getAddr()));
        para.put(i++, new DbParameter(Types.VARCHAR, userActivityRealReward.getPhone()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityRealReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getNo()));
        if (getDb().executeNoneQuery(updateUserActivityRealRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityRealRewardSql = "insert into `t_u_activity_real_reward`(activityId,userId,no,goodsId,getTime,addrTime,addr,phone) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserActivityRealReward userActivityRealReward) {
        userActivityRealReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityRealReward.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityRealReward.getGoodsId()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userActivityRealReward.getGetTime().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userActivityRealReward.getAddrTime().getTime())));
        para.put(i++, new DbParameter(Types.VARCHAR, userActivityRealReward.getAddr()));
        para.put(i++, new DbParameter(Types.VARCHAR, userActivityRealReward.getPhone()));
        if (getDb().executeNoneQuery(insertUserActivityRealRewardSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityRealReward parseData(ResultSet rs) throws SQLException {
        UserActivityRealReward userActivityRealReward = new UserActivityRealReward();
        userActivityRealReward.setActivityId(rs.getInt("activityId"));
        userActivityRealReward.setUserId(rs.getLong("userId"));
        userActivityRealReward.setNo(rs.getInt("no"));
        userActivityRealReward.setGoodsId(rs.getInt("goodsId"));
        userActivityRealReward.setGetTime(rs.getTimestamp("getTime"));
        userActivityRealReward.setAddrTime(rs.getTimestamp("addrTime"));
        userActivityRealReward.setAddr(rs.getString("addr"));
        userActivityRealReward.setPhone(rs.getString("phone"));

        return userActivityRealReward;
    }
}
